package com.blackline.auto.Journal;

import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.Select;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.*;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Journal module.
 */
public class Journal_smoketest {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Journal_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}

	@Test
	public void Journalsmoketest() throws Exception {
		logger.info("this is Journal_smoketest");
		UI.LogonPage.Login(driver, parameters.urlDev2, parameters.getLogonID("preparer"), parameters.getPassword("preparer"));
//		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test3", parameters.getLogonID("preparer"), parameters.getPassword("preparer"));

		Thread.sleep(10000);
		UI.SessionMenu.DownArrow_Periods.click();
		Thread.sleep(2000);
		UI.SessionMenu.selectDate(driver, "228 2011");
		Thread.sleep(6000);

		UI.NavigationMenu.JOURNALS.click();
		Thread.sleep(5000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journals Grid"));
		UI.JournalsGrid.Add_New_Journal_link.click();
		Thread.sleep(5000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journal"));
		UI.Journal.Approver_textField.sendKeys(parameters.getFullName("approver"));
		UI.Journal.Reviewer_textField.sendKeys(parameters.getFullName("reviewer"));		
		UI.Journal.Account_Reviewer_textField.sendKeys(parameters.getFullName("ar"));
		UI.Journal.Account_Manager_textField.sendKeys(parameters.getFullName("am"));
		UI.Journal.Internal_Auditor_textField.sendKeys(parameters.getFullName("ia"));
		UI.Journal.Document_Date_textField.sendKeys("2/01/2011");
		UI.Journal.Document_Type_textField.sendKeys("SA");
		UI.Journal.Posting_Date_textField.sendKeys("2/11/2011");
		UI.Journal.Company_Code_textField.sendKeys("1000");
		String reference=UI.Journal.Reference_TextField.getAttribute("value");
		logger.info("Current Reference is "+reference);
		UI.Journal.Add_Line_Link.click();
		Thread.sleep(10000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journal Line"));
		UI.JournalLine.Pstky_textField.sendKeys("40");
		UI.JournalLine.Account_textField.sendKeys("4000");
		UI.JournalLine.Amount_Document_Currency_textField.sendKeys("10");
		UI.JournalLine.Save_Button.click();
		Thread.sleep(5000);
		logger.info(UI.Journal.PstkyRecord1.getText());
		if (UI.Journal.PstkyRecord1.getText().equals("40"))
			{
				logger.info("[Pass!] New record has been added into Pstky table");
			}
			else
			{
				logger.info("[Fail!] New record is not added into Pstky table");
				verificationErrors.append("[Fail!] New record is not added into Pstky table");
			}
		UI.Journal.Clone_Icon.click();
		Thread.sleep(10000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journal Line"));
		UI.JournalLine.Pstky_textField.clear();
		UI.JournalLine.Pstky_textField.sendKeys("50");
		UI.JournalLine.Save_Button.click();
		Thread.sleep(5000);
		logger.info(UI.Journal.PstkyRecord2.getText());
		if (UI.Journal.PstkyRecord2.getText().equals("50"))
			{
				logger.info("[Pass!] New record has been added into Pstky table");
			}
			else
			{
				logger.info("[Fail!] New record is not added into Pstky table");
				verificationErrors.append("[Fail!] New record is not added into Pstky table");
			}
		if (UI.Journal.Certify_Button.isEnabled())
		{
			logger.info("Certify button is enabled.");
			UI.Journal.Certify_Button.click();
		}
		else
		{
			logger.error("Certify button is NOT enabled.");
			verificationErrors.append("Certify button is NOT enabled.");
		}
		Thread.sleep(5000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journals Grid"));
		
		UI.JournalsGrid.Reference_TextField.sendKeys(reference);
		UI.JournalsGrid.Apply_Link.click();
		if ("View".equals(UI.JournalsGrid.View_link.getText()))
		{
			logger.info("[Pass!] The status is : View, not 'Edit' anymore.");
		}
		else
		{
			logger.error("[Fail!] The status should be 'View', but not.");
			verificationErrors.append("[Fail!] The status should be 'View', but not.");
		}
		UI.SessionMenu.LogOut.click();
		
		UI.LogonPage.LoginID.clear();
		UI.LogonPage.LoginID.sendKeys(parameters.getLogonID("approver"));
		UI.LogonPage.Password.clear();
		UI.LogonPage.Password.sendKeys(parameters.getPassword("approver"));
		UI.LogonPage.LoginButton.click();
		verificationErrors.append(UI.LogonPage.VerifyLogon(parameters.getLogonID("approver"),driver));
		Thread.sleep(10000);
		UI.SessionMenu.DownArrow_Periods.click();
		Thread.sleep(2000);
		UI.SessionMenu.selectDate(driver, "228 2011");
		Thread.sleep(6000);
		
		UI.NavigationMenu.JOURNALS.click();
		UI.JournalsGrid.Reference_TextField.clear();
		UI.JournalsGrid.Reference_TextField.sendKeys(reference);
		UI.JournalsGrid.Apply_Link.click();
		UI.JournalsGrid.Cert_link.click();
		
		UI.Journal.Reject_Button.click();
		//String currentWindow = driver.getWindowHandle();
		Thread.sleep(10000);
		try{
	        new Select(UI.Reconciliation_CIM.Reason_Code_DropdownList).selectByIndex(4);
	        UI.Reconciliation_CIM.Continue_Button.click();
			}catch(NoSuchElementException e){
				logger.info("Not find Reason Code DropdownList");
			}
		Thread.sleep(5000);
		driver.switchTo().frame("Consolidation");
		UI.SendNotificationofRejection.Send_Button.click();
		driver.switchTo().defaultContent();
		Thread.sleep(5000);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Journals Grid"));
		UI.JournalsGrid.Reference_TextField.clear();
		UI.JournalsGrid.Reference_TextField.sendKeys(reference);
		UI.JournalsGrid.Apply_Link.click();
		if ("Rej".equals(UI.JournalsGrid.Rej_link.getText()))
		{
			logger.info("[Pass!] The status is : Rej, not 'Cert' anymore.");
		}
		else
		{
			logger.error("[Fail!] The status should be 'Rej', but not.");
			verificationErrors.append("[Fail!] The status should be 'Rej', but not.");
		}
//		UI.SessionMenu.LogOut.click();
	}

	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}
